<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/7
 * Time: 下午4:05
 */

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Room;
use App\Http\Model\RoomMelee;
use App\Http\Model\RoomScore;


class RoomScoreServices extends CommonServices
{
    private $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new RoomScore();
    }


    /**
     * 设置房间信誉分
     *
     * @param $type int 操作类型：1增加;2减少
     * @param $roomId int 房间id
     * @param $score int 积分
     * @param $desc string 操作描述
     * @param $adminName string 操作者昵称
     * @return array
     */
    public function setRoomReputation($type, $roomId, $score, $desc, $adminName)
    {
        //增减积分
        if (empty($this->model->setRoomReputation($type, $roomId, $score))) {
            OutJson::outJsonNoData(Hint::ROOM_SCORE_ERROR);
        }

        //记录操作日志
        $score = $type == 2 ? 0 - $score : $score; //如果是减分，返回负数
        if (empty($this->model->addRoomScoreLog(1, $roomId, $score, $desc, $adminName))) {
            OutJson::outJsonNoData(Hint::ROOM_SCORE_LOG_ERROR);
        }

        return [];
    }

    /**
     * 还原积分
     * 如果认证状态由整顿中改为已认证，则更新房间积分到默认值
     *
     * @param $roomId int 房间id
     * @return void
     */
    public function resetRoomReputation($roomId)
    {
        $oldScore = $this->model->getReputationByRoomId($roomId);

        $score = $this->model::DEFAULT_REPUTATION - $oldScore;
        if ($score == 0) {
            return;
        }

        //删除记录相当于还原到默认值100分
        $this->model->delRoomReputation($roomId);

        //记录日志
        if (empty($this->model->addRoomScoreLog(1, $roomId, $score, '恢复认证状态，信誉分重置', 'admin'))) {
            OutJson::outJsonNoData(Hint::ROOM_SCORE_LOG_ERROR);
        }

    }

    /**
     * 信誉值更新定时器
     * 房间在一定时间内没有违规，会自动涨积分
     * @return void
     */
    public function roomReputationTimer()
    {

        write_log("roomScore", "roomReputationTimer", ['msg' => '开始执行']);

        $scoreMap = $this->model->getReputationByRange(60, 99); //60-99分才自动涨分
        $roomIds = array_keys($scoreMap);

        $logList = $this->model->getReputationLastLog($roomIds);
        if (count($logList) == 0) {
            write_log("roomScore", "roomReputationTimer", ['msg' => '没有需要更新的记录']);
            return;
        }

        $day = 86400 * 7; //超过七天更新一次
        $addScore = 5;
        foreach ($logList as $log) {
            $time = strtotime($log['created_at']);
            if (time() - $time < $day) {
                continue;
            }

            //加积分
            if ($scoreMap[$log['room_id']] + $addScore > $this->model::DEFAULT_REPUTATION) {
                $score = $this->model::DEFAULT_REPUTATION - $scoreMap[$log['room_id']];
            } else {
                $score = $addScore;
            }

            $this->setRoomReputation(1, $log['room_id'], $score, '连续7日无扣分记录奖励', 'admin');

            write_log("roomScore", "roomReputationTimer", ['msg' => '更新完成', 'log' => $log, 'addScore' => $addScore]);
        }
    }


}